{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "6e30761a",
   "metadata": {},
   "source": [
    "## 1. CandleRule中的相关配置\n",
    "\n",
    "与\"每日定时下载昨日历史K线功能\"重要相关配置为：\n",
    "\n",
    "\n",
    "|参数|类型|默认值|说明|\n",
    "|:---|:---|:---|:---|\n",
    "|**SERVER_WEIGHT**|float, int|0.75|服务占用权重上限的比例 现货交易权重上限1200 合约交易权重上限2400|\n",
    "|**SYMBOLS**|str,list|'all'|收集的产品 all表示全部正在交易的产品|\n",
    "|**SYMBOLS_FILTER**|list|[]|过滤的产品|\n",
    "|**SYMBOL_CONTAINS**|str|''|产品名称中需要包含的内容|\n",
    "|**SYMBOL_ENDSWITH**|str|''|产品名称必须以何内容结尾|\n",
    "|**BAR**|str|binance_interface.binace_lite.BINANCE_DEFAULT_BAR|时间颗粒度|\n",
    "|**TIMEZONE**|str|binance_interface.binace_lite.BINANCE_TIMEZONE|时区|\n",
    "|**CANDLE_DIR**|str|binance_interface.binace_lite.BINANCE_CANDLE_DATE_BASE_DIR|以天为单位的数据存储路径|\n",
    "|**DOWNLOAD_TIME**|str, None|'00:10:00'|每日下载昨日历史K线数据的时刻 格式：%H:%M:%S None表示不下载（每日定时下载K线中使用）|"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "9146e771",
   "metadata": {},
   "source": [
    "## 2. 每日定时下载昨日历史K线"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "9c78fc8b",
   "metadata": {},
   "outputs": [],
   "source": [
    "from binance_interface.app.candle_server import CandleRule,CandleServer\n",
    "import time"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "14ef75eb",
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "2024-01-26 01:16:25,393 [INFO] DOWNLOAD BTCUSD_PERP 1m  24/01/2024\n",
      "2024-01-26 01:16:26,119 [INFO] DOWNLOAD BTCUSD_240329 1m  24/01/2024\n",
      "2024-01-26 01:16:26,769 [INFO] DOWNLOAD BTCUSD_240628 1m  24/01/2024\n",
      "2024-01-26 01:16:27,562 [INFO] DOWNLOAD ETHUSD_PERP 1m  24/01/2024\n",
      "2024-01-26 01:16:28,389 [INFO] DOWNLOAD ETHUSD_240329 1m  24/01/2024\n",
      "2024-01-26 01:16:29,049 [INFO] DOWNLOAD ETHUSD_240628 1m  24/01/2024\n",
      "2024-01-26 01:16:29,769 [INFO] DOWNLOAD LINKUSD_PERP 1m  24/01/2024\n",
      "2024-01-26 01:16:30,560 [INFO] DOWNLOAD BNBUSD_PERP 1m  24/01/2024\n",
      "2024-01-26 01:16:31,290 [INFO] DOWNLOAD TRXUSD_PERP 1m  24/01/2024\n",
      "2024-01-26 01:16:32,109 [INFO] DOWNLOAD DOTUSD_PERP 1m  24/01/2024\n",
      "2024-01-26 01:16:32,940 [INFO] DOWNLOAD ADAUSD_PERP 1m  24/01/2024\n",
      "2024-01-26 01:16:33,607 [INFO] DOWNLOAD EOSUSD_PERP 1m  24/01/2024\n",
      "2024-01-26 01:16:34,328 [INFO] DOWNLOAD LTCUSD_PERP 1m  24/01/2024\n",
      "2024-01-26 01:16:35,082 [INFO] DOWNLOAD BCHUSD_PERP 1m  24/01/2024\n",
      "2024-01-26 01:16:35,809 [INFO] DOWNLOAD XRPUSD_PERP 1m  24/01/2024\n",
      "2024-01-26 01:16:36,618 [INFO] DOWNLOAD ETCUSD_PERP 1m  24/01/2024\n",
      "2024-01-26 01:16:37,389 [INFO] DOWNLOAD FILUSD_PERP 1m  24/01/2024\n",
      "2024-01-26 01:16:38,159 [INFO] DOWNLOAD EGLDUSD_PERP 1m  24/01/2024\n",
      "2024-01-26 01:16:38,888 [INFO] DOWNLOAD DOGEUSD_PERP 1m  24/01/2024\n",
      "2024-01-26 01:16:39,598 [INFO] DOWNLOAD UNIUSD_PERP 1m  24/01/2024\n",
      "2024-01-26 01:16:40,218 [INFO] DOWNLOAD THETAUSD_PERP 1m  24/01/2024\n",
      "2024-01-26 01:16:40,838 [INFO] DOWNLOAD XLMUSD_PERP 1m  24/01/2024\n",
      "2024-01-26 01:16:41,570 [INFO] DOWNLOAD SOLUSD_PERP 1m  24/01/2024\n",
      "2024-01-26 01:16:42,243 [INFO] DOWNLOAD FTMUSD_PERP 1m  24/01/2024\n",
      "2024-01-26 01:16:43,080 [INFO] DOWNLOAD SANDUSD_PERP 1m  24/01/2024\n",
      "2024-01-26 01:16:43,838 [INFO] DOWNLOAD MANAUSD_PERP 1m  24/01/2024\n",
      "2024-01-26 01:16:44,499 [INFO] DOWNLOAD AVAXUSD_PERP 1m  24/01/2024\n",
      "2024-01-26 01:16:45,160 [INFO] DOWNLOAD MATICUSD_PERP 1m  24/01/2024\n",
      "2024-01-26 01:16:45,830 [INFO] DOWNLOAD NEARUSD_PERP 1m  24/01/2024\n",
      "2024-01-26 01:16:46,549 [INFO] DOWNLOAD ATOMUSD_PERP 1m  24/01/2024\n",
      "2024-01-26 01:16:47,230 [INFO] DOWNLOAD AAVEUSD_PERP 1m  24/01/2024\n",
      "2024-01-26 01:16:47,899 [INFO] DOWNLOAD AXSUSD_PERP 1m  24/01/2024\n",
      "2024-01-26 01:16:48,659 [INFO] DOWNLOAD ROSEUSD_PERP 1m  24/01/2024\n",
      "2024-01-26 01:16:49,257 [INFO] DOWNLOAD XTZUSD_PERP 1m  24/01/2024\n",
      "2024-01-26 01:16:49,855 [INFO] DOWNLOAD ICXUSD_PERP 1m  24/01/2024\n",
      "2024-01-26 01:16:50,458 [INFO] DOWNLOAD ALGOUSD_PERP 1m  24/01/2024\n",
      "2024-01-26 01:16:51,187 [INFO] DOWNLOAD RUNEUSD_PERP 1m  24/01/2024\n",
      "2024-01-26 01:16:51,848 [INFO] DOWNLOAD APEUSD_PERP 1m  24/01/2024\n",
      "2024-01-26 01:16:52,507 [INFO] DOWNLOAD KNCUSD_PERP 1m  24/01/2024\n",
      "2024-01-26 01:16:53,207 [INFO] DOWNLOAD XMRUSD_PERP 1m  24/01/2024\n",
      "2024-01-26 01:16:53,927 [INFO] DOWNLOAD GMTUSD_PERP 1m  24/01/2024\n",
      "2024-01-26 01:16:54,729 [INFO] DOWNLOAD OPUSD_PERP 1m  24/01/2024\n",
      "2024-01-26 01:16:55,449 [INFO] DOWNLOAD ENSUSD_PERP 1m  24/01/2024\n",
      "2024-01-26 01:16:56,213 [INFO] DOWNLOAD CHZUSD_PERP 1m  24/01/2024\n",
      "2024-01-26 01:16:57,011 [INFO] DOWNLOAD APTUSD_PERP 1m  24/01/2024\n",
      "2024-01-26 01:16:57,758 [INFO] DOWNLOAD ADAUSD_240329 1m  24/01/2024\n",
      "2024-01-26 01:16:58,428 [INFO] DOWNLOAD LINKUSD_240329 1m  24/01/2024\n",
      "2024-01-26 01:16:59,049 [INFO] DOWNLOAD BCHUSD_240329 1m  24/01/2024\n",
      "2024-01-26 01:16:59,807 [INFO] DOWNLOAD DOTUSD_240329 1m  24/01/2024\n",
      "2024-01-26 01:17:00,498 [INFO] DOWNLOAD XRPUSD_240329 1m  24/01/2024\n",
      "2024-01-26 01:17:01,106 [INFO] DOWNLOAD LTCUSD_240329 1m  24/01/2024\n",
      "2024-01-26 01:17:01,739 [INFO] DOWNLOAD BNBUSD_240329 1m  24/01/2024\n",
      "2024-01-26 01:17:02,371 [INFO] DOWNLOAD ADAUSD_240628 1m  24/01/2024\n",
      "2024-01-26 01:17:02,974 [INFO] DOWNLOAD LINKUSD_240628 1m  24/01/2024\n",
      "2024-01-26 01:17:03,567 [INFO] DOWNLOAD BCHUSD_240628 1m  24/01/2024\n",
      "2024-01-26 01:17:04,217 [INFO] DOWNLOAD DOTUSD_240628 1m  24/01/2024\n",
      "2024-01-26 01:17:04,838 [INFO] DOWNLOAD XRPUSD_240628 1m  24/01/2024\n",
      "2024-01-26 01:17:05,437 [INFO] DOWNLOAD LTCUSD_240628 1m  24/01/2024\n",
      "2024-01-26 01:17:06,037 [INFO] DOWNLOAD BNBUSD_240628 1m  24/01/2024\n",
      "2024-01-26 01:17:06,039 [INFO] COMPLETE DOWNLOAD 24/01/2024 (ALL:59 SKIP:0 SUC:59 WARN:0 ERROR:0)\n"
     ]
    }
   ],
   "source": [
    "# 币币交易：SPOT；U本位合约：UM；币本位合约：CM\n",
    "instType = 'CM'\n",
    "# 永续合约，默认规则\n",
    "candleServer = CandleServer(\n",
    "    instType=instType,\n",
    "    rule=CandleRule,\n",
    "    # 使用http和https代理，proxies={'http':'xxxxx','https:':'xxxxx'}，通requests中的proxies参数规则相同\n",
    "    proxies={},\n",
    "    # 转发：需搭建转发服务器，可参考：https://github.com/pyted/binance_resender\n",
    "    proxy_host=None,\n",
    ")\n",
    "# 每日在CandleRule.DOWNLOAD_TIME时刻开始下载前一日的历史K线\n",
    "candleServer.download_daily(replace=True)  # 异步方法\n",
    "# 等待10秒钟关闭服务\n",
    "time.sleep(10)\n",
    "candleServer.close_download_daily()  # 关闭服务，并不会立刻停止，而是而等待当前正在下载的日期数据全部完成后，停止运行"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.4"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
